<?php

/**
 * controleAtmosphere actions.
 *
 * @package    appcemhti
 * @subpackage controleAtmosphere
 * @author     Steve Cancès
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class controleAtmosphereActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->controle_atmospheres = Doctrine_Core::getTable('controleAtmosphere')
      ->createQuery('a')
      ->execute();
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new controleAtmosphereForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new controleAtmosphereForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($controle_atmosphere = Doctrine_Core::getTable('controleAtmosphere')->find(array($request->getParameter('id_controle_atmosphere'))), sprintf('Object controle_atmosphere does not exist (%s).', $request->getParameter('id_controle_atmosphere')));
    $this->form = new controleAtmosphereForm($controle_atmosphere);
    $this->controleAtmosphere = $controle_atmosphere;
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($controle_atmosphere = Doctrine_Core::getTable('controleAtmosphere')->find(array($request->getParameter('id_controle_atmosphere'))), sprintf('Object controle_atmosphere does not exist (%s).', $request->getParameter('id_controle_atmosphere')));
    $this->form = new controleAtmosphereForm($controle_atmosphere);
    $this->controleAtmosphere = $controle_atmosphere;

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $controle_atmosphere = $form->save();

      $this->redirect('controleAtmosphere/index');
    }
  }
  
  
    public function executeList(sfWebRequest $request)
    {
        if ($request->isXmlHttpRequest())
        {
            $sSearch = $request->getParameter("sSearch");
            $iSortCol_0 = $request->getParameter("iSortCol_0");
            $sSortDir_0 = $request->getParameter("sSortDir_0");
            $iDisplayStart = $request->getParameter("iDisplayStart");
            $iDisplayLength = $request->getParameter("iDisplayLength");
            $page = number_format($iDisplayStart / $iDisplayLength) + 1;
            
            $q = Doctrine_Core::getTable('controleAtmosphere')->getControleAtmosphereDatatable($sSearch, $iSortCol_0, $sSortDir_0);
            
            
            $this->pager = new sfDoctrinePager(
                'controleAtmosphere',
                sfConfig::get(2)
            );
            $this->pager->setQuery($q);
            $this->pager->setMaxPerPage($iDisplayLength);
            $this->pager->setMaxRecordLimit($iDisplayLength);
            $this->pager->setPage($page);
            $this->pager->init();
            
            $pager = $this->pager;

            $this->getContext()->getConfiguration()->loadHelpers('Url'); 
            $aaData = array();
            $list = $pager->getResults();
            foreach ($list as $v)
            {                    
                $aaData[] = array(
                    "0" => $v->getIdControleAtmosphere(),
                    "1" => $v->getLibelle(),
                    "2" => '<a class="submit" href="' . url_for('controleAtmosphere/edit?id_controle_atmosphere='. $v->getIdControleAtmosphere()) . '">Editer</a>',
                );
            }

            $output = array(
            "iTotalRecords" => count($pager),
            "iTotalDisplayRecords" => count($pager),
            "aaData" => $aaData,
            );

            return $this->renderText(json_encode($output));
        }
    }
}
